package com.quick.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.quick.base.BaseTree;
import com.quick.base.Page;
import com.quick.base.Search;
import com.quick.mapper.SysMenuMapper;
import com.quick.service.ISysMenuService;
import com.quick.service.ISysRoleMenuService;
import com.quick.service.ISysUserService;
import com.quick.sys.domain.dto.SysMenuDTO;
import com.quick.sys.domain.pojo.SysMenu;
import com.quick.sys.domain.search.SysMenuSearch;
import com.quick.sys.domain.vo.MenuTreeVo;
import com.quick.sys.domain.vo.SysMenuVo;
import com.quick.sys.domain.vo.SysUserDetailVo;
import com.quick.util.Convert;
import com.quick.util.PageUtil;
import com.quick.util.QuickTool;

import lombok.AllArgsConstructor;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@AllArgsConstructor
public class SysMenuServiceImpl implements ISysMenuService {

    private final SysMenuMapper sysMenuMapper;
    private final ISysUserService sysUserService;
    private final ISysRoleMenuService roleMenuService;

	@Override
    public List<MenuTreeVo> loadMenuTree() {
    	List<MenuTreeVo> result = new ArrayList<MenuTreeVo>();
    	SysUserDetailVo res = sysUserService.getUser();
    	List<Integer> menuIds = roleMenuService.selectMenuByRoles(res.getRoles());
    	Map<Integer, Object> menuMap = new HashMap<Integer, Object>();
    	for (Integer menu : menuIds) {
			menuMap.put(menu, menu);
		}
    	List<MenuTreeVo> trees = sysMenuMapper.loadMenuNotFunctionTree(-1);
    	for (MenuTreeVo tree : trees) {
    		//一级菜单
			if(menuMap.containsKey(tree.getId())) {
				if(tree.getChildren().size()>0) {
					//二级菜单
					List<MenuTreeVo> children = new ArrayList<MenuTreeVo>();
					for (MenuTreeVo child : tree.getChildren()) {
						if(menuMap.containsKey(child.getId())){
							if(child.getChildren().size()>0) {
								List<MenuTreeVo> levels = new ArrayList<MenuTreeVo>();
								for (MenuTreeVo level : child.getChildren()) {
									if(menuMap.containsKey(level.getId())){
										levels.add(level);
									}
								}
								child.setChildren(levels);
								children.add(child);
							}else {
								children.add(child);
							}
						}
					}
					tree.setChildren(children);
					result.add(tree);
				}else {
					result.add(tree);
				}
			}
		}
        return result;
    }

    @Override
    public List<SysMenuVo> loadAllMenu() {
        QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<>();
        List<SysMenu>  menus = sysMenuMapper.selectList(queryWrapper);
        return Convert.convert(menus,SysMenuVo.class);
    }

    @Override
    public Boolean saveOrUpdate(SysMenuDTO sysMenu) {
        SysMenu entity = Convert.convert(sysMenu,SysMenu.class);
        if(QuickTool.isNull(entity.getId())){
        	QuickTool.setFillInsert(entity);
            return sysMenuMapper.insert(entity)>0;
        }
        QuickTool.setFillUpdate(entity);
        return sysMenuMapper.updateById(entity)>0;
    }
    @Override
    public SysMenuVo getMenuById(Integer id){
        SysMenu menu = sysMenuMapper.selectById(id);
        return Convert.convert(menu, SysMenuVo.class);
    }

    @Override
    public Boolean deleteMenu(Integer id) {
        return sysMenuMapper.deleteById(id) > 0;
    }

	@Override
	public Page<SysMenuVo> searchMenu(Search<SysMenuSearch> search) {
		QueryWrapper<SysMenu> queryWrapper = new QueryWrapper<SysMenu>();
		queryWrapper(queryWrapper,search.getSearch());
		List<SysMenu> menus = sysMenuMapper.selectList(queryWrapper);
		PageInfo<SysMenu> pageInfo = new PageInfo<>(menus);
		return PageUtil.page(pageInfo, SysMenuVo.class);
	}

	@Override
	public List<BaseTree> loadBaseTree() {
		return sysMenuMapper.loadBaseTree(0);
	}

	@Override
	public List<MenuTreeVo> loadAdminMenuTree() {
		List<MenuTreeVo> trees = sysMenuMapper.loadMenuTree(-1);
		return trees;
	}

}
